//
//  personInfodb.m
//  tcpoctest
//
//  Created by apple on 13-3-11.
//  Copyright (c) 2013年 gump. All rights reserved.
//

#import "personInfodb.h"

@implementation personInfodb

-(void)creatTable
{
    if (db == nil)
    {
        return;
    }
    
    pthread_mutex_lock(&mutexdatabase);
    
    [db open];
    
    [db executeUpdate:@"create table if not exists firendinfo(mail text,\
name text,province char(1),city char(1),sex char(1),\
age char(1),coin int,sign text,ppnumber int,glamour int)"];
    
    [db close];
    
    pthread_mutex_unlock(&mutexdatabase);
}

-(void)insertInfo:(FirendInfo *)fi
{
    if (db == nil)
    {
        return;
    }
    
    NSString *strsql = [NSString stringWithFormat:@"SELECT * FROM firendinfo \
WHERE mail = '%@'",fi.mail];
    
    pthread_mutex_lock(&mutexdatabase);
    
    [db open];
    
    FMResultSet *s = [db executeQuery:strsql];
    if([s next])
    {
        strsql = [NSString stringWithFormat:@"update firendinfo \
set name = '%@',province = '%d',city = '%d',sex = '%d',age = '%d',\
coin = %d,sign = '%@',ppnumber = %d \
WHERE mail = '%@'",
                  fi.name
                  ,fi.province
                  ,fi.city
                  ,fi.sex
                  ,fi.age
                  ,fi.iCoin
                  ,fi.strSign
                  ,fi.ippnum
                  ,fi.mail];
    }
    else
    {
        strsql = [NSString stringWithFormat:@"INSERT INTO firendinfo \
(mail,name,province,city,sex,age,coin,sign,ppnumber,glamour)\
VALUES ('%@','%@','%d','%d','%d','%d',%d,'%@',%d,%d)"
                  ,fi.mail
                  ,fi.name
                  ,fi.province
                  ,fi.city
                  ,fi.sex
                  ,fi.age
                  ,fi.iCoin
                  ,fi.strSign
                  ,fi.ippnum
                  ,fi.iGlamour];
        
        
    }
    
    [db executeUpdate:strsql];
    [db close];
    
    pthread_mutex_unlock(&mutexdatabase);
}

-(void)deleteInfo:(NSString *)mail
{
    if (db == nil)
    {
        return;
    }
    
    NSString *strsql = [NSString stringWithFormat:@"DELETE FROM firendinfo WHERE mail='%@'"
                        ,mail];
    
    pthread_mutex_lock(&mutexdatabase);
    
    [db open];
    [db executeUpdate:strsql];
    [db close];
    
    pthread_mutex_unlock(&mutexdatabase);
}

-(void)initData
{
    if (db == nil) {
        return;
    }
    
    pthread_mutex_lock(&mutexdatabase);
    
    [db open];
    
    NSString *strsql = [NSString stringWithFormat:@"SELECT \
mail,name,province,city,sex,age,coin,sign,ppnumber,glamour \
FROM firendinfo"];
    FMResultSet *s = [db executeQuery:strsql];
    
    [[DataControl shareControl].firendInfoDic removeAllObjects];
    
    while ([s next])
    {
        FirendInfo *fi = [[FirendInfo alloc] init];
        
        fi.mail = [s stringForColumn:@"mail"];
        fi.name = [s stringForColumn:@"name"];
        fi.province = [s intForColumn:@"province"];
        fi.city = [s intForColumn:@"city"];
        fi.sex = [s intForColumn:@"sex"];
        fi.age = [s intForColumn:@"age"];
        fi.iCoin = [s intForColumn:@"coin"];
        fi.strSign = [s stringForColumn:@"sign"];
        fi.ippnum = [s intForColumn:@"ppnumber"];
        fi.iGlamour = [s intForColumn:@"glamour"];
        
        [[DataControl shareControl].firendInfoDic setObject:fi forKey:fi.mail];
    }
    
    [db close];
    
    pthread_mutex_unlock(&mutexdatabase);
}

@end
